home *** CD-ROM | disk | FTP | other *** search
- /*
- IC Helper What.c
-
- */
-
- #include <LowMem.h>
- #include <Lists.h>
- #include <Icons.h>
-
- #include "IC Window Globals.h"
- #include "IC Types.h"
- #include "IC Keys.h"
- #include "IC API.h"
- #include "IC Subs.h"
- #include "IC Dialogs.h"
- #include "IC Misc Subs.h"
- #include "IC Document.h"
- #include "IC Globals.h"
- #include "IC IconSuites.h"
- #include "IC StandardFile.h"
- #include "IC Movable Modal.h"
- #include "IC Windows.h"
-
- #include "IC Helper What.h"
-
- void GetNthElement(Handle entries,short index,InternalAppSpec* res){
- BlockMoveData((Ptr)(((long)(*entries))+((index-1)*sizeof(InternalAppSpec))),res,sizeof(InternalAppSpec));
- }
-
- void SetNthElement(Handle entries,short index,InternalAppSpec* res){
- BlockMoveData((Ptr)res,(Ptr)(((long)(*entries))+((index-1)*sizeof(InternalAppSpec))),sizeof(InternalAppSpec));
- }
-
- Boolean EntryLocked(Handle entries,short index){
- InternalAppSpec appspec;
-
- GetNthElement(entries,index,&appspec);
- return appspec.locked;
- }
-
- void DimHelperButtons(WindowType wt){
- DialogPtr dlg=WindowInfo[wt].window;
- Handle entries=(Handle)WindowInfo[wt].items[ditHList]->data;
- ListHandle lh=(ListHandle)WindowInfo[wt].items[ditHList]->spare_data;
- short selection=SelectedLine(lh);
- Boolean have_selection=(selection!=-1),elock=EntryLocked(entries,selection+1);
-
- SetDCtlEnable(dlg,ditHAdd,!(IsDocLocked()));
- SetDCtlEnable(dlg,ditHDelete,(have_selection)&&(!elock));
- SetDCtlEnable(dlg,ditHChange,(have_selection)&&(!elock));
- OutlineDefault1(dlg,ditHMainUserItem);
- }
-
- pascal void HelperUserItemUpdate(DialogPtr dlg,short item){
- Rect list_rect;
-
- GetDItemRect(dlg,item,&list_rect);
- PenNormal();
- InsetRect(&list_rect,-1,-1);
- FrameRect(&list_rect);
- LUpdate(dlg->visRgn,(ListHandle)WindowInfo[GetWindowType(dlg)].items[item]->spare_data);
- }
-
- void HelperLDDraw(Handle entries,Boolean select,Rect* r,Cell the_cell,short offset,short datalen,ListHandle lh){
- InternalAppSpec appspec;
- OSErr err;
- Handle suite;
- short cur_x;
- Rect tmprect;
- FontInfo sys_font_info;
- short top,transfer;
- RgnHandle rgn,rgn2;
- Str255 ts1=kICHelper,ts2;
-
- if (datalen==0){
- GetNthElement(entries,the_cell.v+1,&appspec);
- EraseRect(r);
- cur_x=0;
- rgn=NewRgn();
- if (System7){
- tmprect.top=r->top;
- tmprect.left=r->left;
- tmprect.bottom=tmprect.top+32;
- tmprect.right=tmprect.left+32;
-
- OffsetRect(&tmprect,((kCellHeight-32)/2),((kCellHeight-32)/2));
-
- GetDTDBIconSuiteCached('APPL',appspec.appspec.fCreator,&suite);
-
- if (suite!=(Handle)0){
- if (select)
- transfer=ttSelected;
- else
- transfer=ttNone;
-
- err=PlotIconSuite(&tmprect,atNone,transfer,suite);
- IconSuiteToRgn(rgn,&tmprect,atNone,suite);
- }
- cur_x+=kCellHeight;
- }
- cur_x+=2;
-
- TextFont(systemFont);
- GetFontInfo(&sys_font_info);
-
- top=((kCellHeight-(sys_font_info.ascent+sys_font_info.descent))/2);
-
- TextFont(systemFont);
- MoveTo(r->left+cur_x,r->top+top+sys_font_info.ascent);
-
- // ts1 is temp holder of kICHelper
- // first copy the key to the appspec key
- TPCopy(ts1,appspec.key,ts1[0]+1,255);
-
- // combine the key, " - ", and the app's name, then draw to right of icon element
- GetAString(ts2,128,5);
-
- Concat(ts1,2,ts2,appspec.appspec.name);
-
- BlockMoveData((Ptr)&(ts2[1]),(Ptr)&(ts1[ts1[0]+1]),ts2[0]);
- ts1[0]+=ts2[0];
- BlockMoveData((Ptr)&(appspec.appspec.name[1]),(Ptr)&(ts1[ts1[0]+1]),appspec.appspec.name[0]);
- ts1[0]+=appspec.appspec.name[0];
-
- DrawString(ts1);
-
- if (select){
- rgn2=NewRgn();
- RectRgn(rgn2,r);
- if (Has_ColorQD){
- unsigned char hilitemode=LMGetHiliteMode();
-
- BitClr(&hilitemode,pHiliteBit);
- LMSetHiliteMode(hilitemode);
- }
-
- XorRgn(rgn,rgn2,rgn2);
-
- InvertRgn(rgn2);
- DisposeRgn(rgn2);
- }
-
- if (rgn!=(RgnHandle)0)
- DisposeRgn(rgn);
- }
- }
-
- pascal void HelperLDEF(short message,Boolean select,Rect* r,Cell the_cell,short offset,short datalen,ListHandle lh){
- Handle entries;
-
- SetPort((*lh)->port);
- entries=(Handle)WindowInfo[GetWindowType((*lh)->port)].items[ditHList]->data;
-
- switch (message){
- case lInitMsg:
- break;
- case lDrawMsg:
- case lHiliteMsg:
- HelperLDDraw(entries,select,r,the_cell,offset,datalen,lh);
- break;
- case lCloseMsg:
- break;
- default:
- break;
- }
- }
-
- OSErr WhatOpenHelper(WindowType wt,short item){
- DialogPtr dlg;
- Handle entries;
- long attr;
- OSErr err=noErr;
- long count,ndx,size;
- Rect list_rect,data_rect;
- InternalAppSpec tmpapspec;
- ListHandle lh;
- Point cell_size;
- Str255 tstr,ich=kICHelper;
-
- WindowInfo[wt].items[item]->spare_data=0;
- WindowInfo[wt].items[item]->data=(Ptr)0;
- dlg=WindowInfo[wt].window;
- SetDItemHandle(dlg,item,(Handle)gHelperUserItemUpdate);
- entries=NewHandle(0);
- if (entries==(Handle)0)
- err=memFullErr;
- else {
- ndx=1;
- err=noErr;
- do {
- err=ICMapErr(ICGetIndPref(GetInstance(),ndx,tmpapspec.key));
- if (err==noErr){
- // extract the first chars from the string to see if it begins with helper...
- TPCopy(tstr,tmpapspec.key,1,ich[0]);
- if (IUEqualString(tstr,ich)==0){
- size=sizeof(ICAppSpec);
- err=ICMapErr(ICGetPref(GetInstance(),tmpapspec.key,&attr,(Ptr)&(tmpapspec.appspec),&size));
- if (err==noErr){
- tmpapspec.locked=BitTst(&attr,ICattr_locked_bit)||IsDocLocked();
- err=PtrAndHand(&tmpapspec,entries,sizeof(InternalAppSpec));
- }
- }
- }
- ndx++;
- } while (err==noErr);
- }
-
- if (err==icPrefNotFoundErr)
- err=noErr;
-
- if (err==noErr){
- count=GetHandleSize(entries)/sizeof(InternalAppSpec);
- WindowInfo[wt].items[item]->data=(Ptr)entries;
- GetDItemRect(dlg,item,&list_rect);
- list_rect.right-=15;
- SetRect(&data_rect,0,0,1,count);
- SetPt(&cell_size,list_rect.right-list_rect.left,kCellHeight);
- lh=LNew(&list_rect,&data_rect,cell_size,128,dlg,true,false,false,true);
- if (lh==(ListHandle)0)
- err=memFullErr;
- }
-
- if (err==noErr){
- (*lh)->refCon=(long)gHelperLDEF;
- (*lh)->selFlags=lOnlyOne;
- WindowInfo[wt].items[item]->spare_data=(Ptr)lh;
- DimHelperButtons(wt);
- SetUpDefaultOutline(dlg,ditHChange,ditHMainUserItem);
- WindowInfo[wt].selected_item=ditHList;
- }
-
- return err;
- }
-
- Boolean ChooseApplication(ICAppSpec* spec){
- FInfo info;
- FSSpec fs;
- OSErr err;
-
- err=ICStandardGetFile('APPL',&fs,&info);
- DisplayError(acChooseApplication,err);
- if (err==noErr){
- spec->fCreator=info.fdCreator;
- SetPString(spec->name,1,fs.name);
- }
-
- return err;
- }
-
- void FixButton(DialogPtr dlg){
- Str255 t1,t2;
-
- GetItemText(dlg,ditHAppName,t1);
- GetItemText(dlg,ditHHelper,t2);
-
- SetDCtlEnable(dlg,ditHOK,((t1[0]!=0)&&(t2[0]!=0)));
- OutlineDefault1(dlg,ditHAddUserItem);
- }
-
- pascal Boolean DoAddFilter(DialogPtr dlg,EventRecord* er,short* item){
- Boolean res=CancelModalFilter(dlg,er,item);
-
- FixButton(dlg);
-
- return res;
- }
-
- /*
- DoEdit
-
- Pops up a dialog to allow someone to choose a new application or change a helper string.
- */
- OSErr DoEdit(Handle entries,ListHandle lh,InternalAppSpec* choosen_app){
- DialogPtr dlg=GetNewDialog(800,(Ptr)0,(WindowPtr)-1);
- short item;
- OSErr err=noErr;
- Ptr saved_state;
-
- if (dlg==(DialogPtr)0)
- err=memFullErr;
- else {
- SetUpDefaultOutline(dlg,ditHOK,ditHAddUserItem);
- SetItemText(dlg,ditHHelper,choosen_app->key);
- SetItemText(dlg,ditHAppName,choosen_app->appspec.name);
- SelectDialogItemText(dlg,ditHHelper,0,255);
- FixButton(dlg);
- ShowWindow(dlg);
- DisableMenuBar(&saved_state,-1);
- InitCursor();
-
- do {
- MovableModalDialog(gDoAddFilter,&item);
-
- if (item==ditHChooseApplication){
- // if we choose an application with no errors, fix up the dialog
- if (ChooseApplication(&(choosen_app->appspec))==noErr){
- SetItemText(dlg,ditHAppName,choosen_app->appspec.name);
- FixButton(dlg);
- }
- }
- } while ((item!=ditHOK)&&(item!=ditHCancel));
-
- if (item==ditHOK){
- Str255 tstr;
-
- GetItemText(dlg,ditHHelper,tstr);
- SetPString(choosen_app->key,2,kICHelper,tstr);
- } else
- err=userCanceledErr;
-
- ReEnableMenuBar(&saved_state);
- DisposeDialog(dlg);
- }
- return err;
- }
-
- OSErr DoAdd(Handle entries,ListHandle lh){
- InternalAppSpec choosen_app;
- OSErr err;
-
- SetPString(choosen_app.appspec.name,0);
- choosen_app.locked=false;
- SetPString(choosen_app.key,0);
-
- err=DoEdit(entries,lh,&choosen_app);
-
- if (err==noErr){
- err=PtrAndHand(&choosen_app,entries,sizeof(InternalAppSpec));
- if (err==noErr)
- LAddRow(1,32767,lh);
- }
-
- return err;
- }
-
- OSErr DoChange(short selection,Handle entries,ListHandle lh){
- OSErr err;
- InternalAppSpec tmpspec;
- Str255 tmp=kICHelper;
-
- GetNthElement(entries,selection+1,&tmpspec);
- Delete(tmpspec.key,1,tmp[0]);
-
- err=DoEdit(entries,lh,&tmpspec);
- if (err==noErr)
- SetNthElement(entries,selection+1,&tmpspec);
- else
- err=userCanceledErr;
-
- return err;
- }
-
- /*
- Oops, small bug in the pascal version, corrected here. When you add a new
- helper, the actual add to the prefs file is delayed until the window is flushed.
-
- However, deletes from the prefs file are done when the button is pressed.
-
- The bug occurs when you want to delete an entry which has just been added but
- not saved (I don't know of a logical situation when this could occur, but it is possible).
- The removal from the list only takes place if the removal from the prefs file is
- successful. The two should be logically separated in some way. I chose to ignore
- the 'resource not found' error and just remove it from the list.
- */
- OSErr DoDelete(short selection,Handle entries,ListHandle lh){
- OSErr err,err2;
- InternalAppSpec apspec;
- long pos;
-
- err=ICMapErr(ICBegin(GetInstance(),icReadWritePerm));
- if (err==noErr){
- GetNthElement(entries,selection+1,&apspec);
- err=ICMapErr(ICDeletePref(GetInstance(),apspec.key));
- ICEnd(GetInstance());
- }
- if ((err==noErr)||(err==icPrefNotFoundErr)){
- Munger(entries,selection*sizeof(InternalAppSpec),(Ptr)0,sizeof(InternalAppSpec),(Ptr)-1,0);
- err=MemError();
- if (err==noErr)
- LDelRow(1,selection,lh);
- }
-
- return err;
- }
-
-
- OSErr WhatClickHelper(WindowType wt,short item,EventRecord* er){
- ListHandle lh=(ListHandle)WindowInfo[wt].items[ditHList]->spare_data;
- Handle entries=(Handle)WindowInfo[wt].items[ditHList]->data;
- short selection=SelectedLine(lh);
- OSErr err=(OSErr)1;
-
- GlobalToLocal(&er->where);
-
- switch (item){
- case ditHAdd:
- err=DoAdd(entries,lh);
- break;
- case ditHChange:
- err=DoChange(selection,entries,lh);
- break;
- case ditHDelete:
- if (selection!=-1)
- err=DoDelete(selection,entries,lh);
- break;
- case ditHList:
- if (LClick(er->where,0,lh)){
- selection=SelectedLine(lh);//selection may have been changed by LClick
- if (EntryLocked(entries,selection+1)){
- LockedAlert(wt,item);
- err=userCanceledErr;
- } else {
- FlashItem(WindowInfo[wt].window,ditHChange);
- err=DoChange(selection,entries,lh);
- }
- }
- break;
- default:
- break;
- }
-
- if (err==noErr)
- DirtyDocument();
-
- if (err==(OSErr)1)
- err=noErr;
-
- DimHelperButtons(wt);
-
- return err;
- }
-
- OSErr WhatFlushHelper(WindowType wt,short item){
- OSErr err=noErr,first_err=noErr;
- Handle entries=(Handle)WindowInfo[wt].items[item]->data;
- short i;
- InternalAppSpec appspec;
-
- for (i=1;i<(GetHandleSize(entries)/sizeof(InternalAppSpec));i++){
- GetNthElement(entries,i,&appspec);
- if (!appspec.locked)
- err=ICMapErr(ICSetPref(GetInstance(),appspec.key,ICattr_no_change,(Ptr)&(appspec.appspec),sizeof(ICAppSpec)));
-
- if (err!=noErr)
- if (first_err==noErr)
- first_err=err;
- }
- return first_err;
- }
-
- OSErr WhatCloseHelper(WindowType wt,short item){
- LDispose((ListHandle)WindowInfo[wt].items[item]->spare_data);
- DisposeHandle((Handle)WindowInfo[wt].items[item]->data);
- return noErr;
- }
-
- OSErr WhatActivateHelper(WindowType wt,short item,Boolean activate){
- LActivate(activate,(ListHandle)WindowInfo[wt].items[item]->spare_data);
- return noErr;
- }
-
- pascal StringPtr GetHelpEntryName(ListHandle list,Cell c,StringPtr str){
- InternalAppSpec appspec;
- Handle entries=(Handle)WindowInfo[GetWindowType((*list)->port)].items[ditHList]->data;
- Str255 tstr,ich=kICHelper,astr;
-
- GetNthElement(entries,c.v+1,&appspec);
- GetAString(astr,128,5);
-
- SetPString(str,3,TPCopy(tstr,appspec.key,ich[0]+1,255),astr,appspec.appspec.name);
-
- return str;
- }
-
- OSErr WhatKeyHelper(WindowType wt,short item,EventRecord* er){
- OSErr err=noErr;
- char ch;
- ListHandle lh;
- Handle entries;
- short selection;
- Str255 tstr;
-
- if ((er->what==keyDown)||(er->what==autoKey)){
- ch=er->message&0x00ff;
-
- switch (ch){
- case crChar:
- case enterChar:
- if (GetDCtlEnable(WindowInfo[wt].window,ditHChange)){
- entries=(Handle)WindowInfo[wt].items[ditHList]->data;
- lh=(ListHandle)WindowInfo[wt].items[item]->spare_data;
- selection=SelectedLine(lh);
- FlashItem(WindowInfo[wt].window,ditHChange);
- err=DoChange(selection,entries,lh);
- if (err==noErr)
- DirtyDocument();
- }
- break;
- default:
- DoListKey((ListHandle)WindowInfo[wt].items[item]->spare_data,er,gGetHelpEntryName);
- DimHelperButtons(wt);
- break;
- }
- }
-
- return err;
- }
-
- OSErr WhatCursorHelper(WindowType wt,short item,Point pt,short cursorid){
- Rect r;
- CursHandle curs;
-
- GetDItemRect(WindowInfo[wt].window,item,&r);
- InsetRect(&r,15,0);
-
- if (PtInRect(pt,&r)){
- curs=GetCursor(cursorid);
- SetCursor(*curs);
- } else
- InitCursor();
-
- return noErr;
- }
-
- void HelperSetSelection(StringPtr key){
- long i;
- WindowPtr window;
- Handle entries;
- InternalAppSpec appspec;
- ListHandle list;
- Str255 tstr;
-
- SetPString(tstr,2,kICHelper,key);
- window=GetWindowPtr(WT_Helper);
- if (window!=(WindowPtr)0){
- list=(ListHandle)WindowInfo[WT_Helper].items[ditHList]->spare_data;
- entries=(Handle)WindowInfo[WT_Helper].items[ditHList]->data;
-
- for (i=1;i<=(GetHandleSize(entries)/sizeof(InternalAppSpec));i++){
- GetNthElement(entries,i,&appspec);
- if (IUEqualString(appspec.key,tstr)==0){
- LSetSingleSelection(list,i-1);
- DimHelperButtons(WT_Helper);
-
- if (GetDCtlEnable(window,ditHChange))
- DoChange(i-1,entries,list);
- }
- }
- }
- }
-